﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UpdateFileContent.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the UpdateFileContent type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Wss.File
{
    using System.Collections.Generic;

    using Entities;
    using Engine;
    using SharePoint.Wss.Files;
    using Utils;

    using SharePoint.Wss.Attributes;

    /// <summary>
    /// This Action is reponsible for updating the file content in the list.
    /// </summary>
    /// <remarks>t
    /// Some Rules :
    /// This will update the checked out file only.
    /// </remarks>
    [CollaborisClass(
        Title = "Update File Content",
        ReleaseStatus = ReleaseStatus.Stable,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Updates the file content in a list.",
        Remarks = @"File content will be updated only if the file is checked-out. Hence, please check out the file prior to running. " +
                  @"You may wish to preceed this action with <a href=""Collaboris.Saf.Actions.Wss.File.CheckOutFile.html"">Check-out File action</a>",
        ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C650}",
        UndoImplemented = false,
        SupportFiles = new string[] { @"File\_support\default.aspx" }
    )
    ]
    public class UpdateFileContent : WSSActionBase<ListFileInfo>
    {
        #region Methods (6) 

        /// <summary>
        /// This method will update the file content.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, ListFileInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(TargetWeb, "TargetWeb");
            ValidationHelper.VerifyObjectArgument(entity, "entity");

            ProcessStatus status = this.UpdateSPFileContent(entity, ProcessMode.Do);

            this.Trace.TraceMethodEnd("DoAction");

            return status;
        }

        /// <summary>
        /// This method will "Undo" the Current file content.
        /// </summary>
        /// <param name="context">
        /// IContext defining the current context.
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// ProcessStatus signalling to the Macro what its outcome was.
        /// </returns>
        //protected override ProcessStatus UndoAction(SafContext context, ListFileInfo entity)
        //{
        //    this.Trace.TraceMethodStart("UndoAction");
            
        //    ValidationHelper.VerifyObjectArgument(TargetWeb, "TargetWeb");
        //    ValidationHelper.VerifyObjectArgument(entity, "entity");

        //    ProcessStatus status = this.UpdateSPFileContent(entity, ProcessMode.Undo);

        //    this.Trace.TraceMethodEnd("UndoAction");

        //    return status;
        //}

        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, ListFileInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.            
            ValidateStringForNull(entity.DiskFilePath, "ListFileInfo.DiskFilePath", ref errors);                       
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }

        /// <summary>
        /// Updates the specified the file content as spec'd in <see cref="ListItemFileInfo"/>
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="web">The web.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        private ProcessStatus UpdateSPFileContent(ListFileInfo entity, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("UpdateSPFileContent");                        

            bool bSuccess = false;
            if (mode == ProcessMode.Do)
            {                
                bSuccess = SPFileHelper.UpdateFileContent(TargetWeb,entity.FileUrls,entity.DiskFilePath);
            }
            if (mode == ProcessMode.Undo)
            {
                // TODO: Do we need to get the List of files from the macro report?
                bSuccess = SPFileHelper.RevertToOldFileContent(TargetWeb, entity.FileUrls, entity.DiskFilePath);
            }

            this.Trace.TraceMethodEnd("UpdateSPFileContent");

            if (bSuccess)
            {
                return ProcessStatus.Success;
            }
            else
            {
                return ProcessStatus.Error;
            }

        }

        #endregion Methods 
    }
}